﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using System.Data;
using BLL;
using Utils;

namespace UI.ModEmployee
{
    public partial class DefRewardsTypeAdd : System.Web.UI.Page
    {
        DefDicBiz defineDictionary = new DefDicBiz();
        string id = "0";//By Update
        string tableName = "Admin_Rewards_Type";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Params["ID"] != null)
            {
                id = Request.Params["ID"].ToString();
                btnAdd.Enabled = false;
            }
            else
            {
                btnUpdate.Enabled = false;
                btnDelete.Enabled = false;
            }

            if (!IsPostBack)
            {
                if (id != "0")
                    BindData(id);
               
            }
        }
        private void BindData(string id)
        {
            DataTable dt = defineDictionary.GetOneInfo(tableName, id);
            if (dt.Rows.Count > 0)
            {
                txtMemo.Text = dt.Rows[0]["Memo"].ToString();
                txtAbsIndex.Text = dt.Rows[0]["AbsIndex"].ToString();
                txtName.Text = dt.Rows[0]["Name"].ToString();
                defineDictionary.InitDropDownList(ddlTypeName, dt.Rows[0]["TypeName"].ToString());
                defineDictionary.InitDropDownList(ddlUnit, dt.Rows[0]["Unit"].ToString());
            }
            else
            {
                JSUtility.Alert(this, "无数据");
            }

        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string name = txtName.Text.Trim();
            string typeName = ddlTypeName.SelectedItem.Text;
            string unit = ddlUnit.SelectedItem.Text;
            string memo = txtMemo.Text.Trim();
            string absIndex = txtAbsIndex.Text.Trim();
            if (string.IsNullOrWhiteSpace(name))
            {
                JSUtility.Alert(this, "名称不能为空！");
                return;
            }
            if (defineDictionary.IsExistOneInfo(tableName, name))
            {
                JSUtility.Alert(this, "名称已经使用，请输入新名称！\n（如果该名称被关闭，你可以打开它）");
                return;
            }
            if (!Utils.RegexRule.IsInt(absIndex) || !Utils.RegexRule.IsDecimal(memo))
            {
                JSUtility.Alert(this, "金额和索引一定要填写数字！");
                return;
            }

            SortedDictionary<string, string> sdList = new SortedDictionary<string, string>();
            sdList.Add("Name", name);
            sdList.Add("TypeName", typeName);
            sdList.Add("Memo", memo);

            sdList.Add("Unit", unit);
            sdList.Add("AbsIndex", absIndex);
            defineDictionary.Insert(tableName, sdList);
            JSUtility.AlertAndRedirect("增奖惩类别成功", "DefRewardsTypeAdd.aspx");
          
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            string name = txtName.Text.Trim();
            string typeName = ddlTypeName.SelectedItem.Text;
            string unit = ddlUnit.SelectedItem.Text;
            string memo = txtMemo.Text.Trim();
            string absIndex = txtAbsIndex.Text.Trim();
            if (string.IsNullOrWhiteSpace(name))
            {
                JSUtility.Alert(this, "名称不能为空！");
                return;
            }
          
            if (!Utils.RegexRule.IsInt(absIndex) || !Utils.RegexRule.IsDecimal(memo))
            {
                JSUtility.Alert(this, "金额和索引一定要填写数字！");
                return;
            }

            SortedDictionary<string, string> sdList = new SortedDictionary<string, string>();
            sdList.Add("ID", id);
            sdList.Add("Name", name);
            sdList.Add("TypeName", typeName);
            sdList.Add("Memo", memo);
            sdList.Add("Unit", unit);
            sdList.Add("AbsIndex", absIndex);

            defineDictionary.Update(tableName, sdList);

            JSUtility.Alert(this, "更新成功！");
            JSUtility.RefreshParent();
            JSUtility.CloseTimeOut();
                   
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {

            if (id != "0")
            {
                string info = defineDictionary.Delete(tableName, new string[] { "ID", id, "N" });
                JSUtility.Alert(this, info);
                JSUtility.RefreshParent();
                JSUtility.CloseTimeOut();
            }
            else
                JSUtility.Alert(this, "请先选择数据");
        }


    }
}